$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'systemVersion/systemversion/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', width: 20, key: true, align: 'center'},
            {label: '版本号 ', name: 'versionNo', index: 'version_no', width: 30, align: 'center'},
            {label: '版本名字', name: 'versionName', index: 'version_name', width: 50, align: 'center'},
            {label: '平台来源', name: 'appType', index: 'app_type', width: 30, align: 'center'},
            {label: '是否强制更新', name: 'versionForce', index: 'version_force', width: 30, align: 'center'},
            {label: '下载地址', name: 'downloadUrl', index: 'download_url', width: 150},
            {label: '结束时间', name: 'versionTime', index: 'version_time', width: 80, align: 'center'},
            {label: '备注', name: 'versionContent', index: 'version_content', width: 80},
            {label: '最小版本', name: 'versionMin', index: 'version_min', width: 80, align: 'center'}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        systemVersion: {}
    },
    methods: {
        checkEndDate: function () {
            WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:false, onpicking:function (dp) {
                    vm.systemVersion.versionTime = dp.cal.getNewDateStr();
                }})
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.systemVersion = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            if (!vm.systemVersion.versionNo||!vm.systemVersion.versionName||!vm.systemVersion.downloadUrl||!vm.systemVersion.versionTime||!vm.systemVersion.versionContent||!vm.systemVersion.versionMin) {
                alert('必填项不能为空!')
                return
            }
            var url = vm.systemVersion.id == null ? "systemVersion/systemversion/save" : "systemVersion/systemversion/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.systemVersion),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "systemVersion/systemversion/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "systemVersion/systemversion/info/" + id, function (r) {
                vm.systemVersion = r.systemVersion;
            });
        },
        reload: function (event) {
            vm.showList = true;
            $("#jqGrid").jqGrid('setGridParam', {
                page: 1
            }).trigger("reloadGrid");
        }
    }
});